const app = getApp()
const request = require('../../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories: [],
    tagList: [],
    fileList: [],
    tagIdForQuery: "10", //初始页面的查询id值，后续点击会改变
  },

  tabChangeIndex(e) {
    console.log(e)

    this.setData({
      idx: e.detail.idx,
      tagIdForQuery: e.detail.tag,
      fileList: []
    })
    this.queryLibraryByTagId();
  },


  onLoad: function(options) {
    this.queryTagList();
    this.queryLibraryByTagId();
    console.log(this.data.fileList)
  },

  queryLibraryByTagId: function() {
    var that = this;
    const data = {
      url: '/v3/file/file_lib',
      method: 'POST',
      param: {
        'pageNum': 1,
        'pageSize': 20,
        'primaryTagId': that.data.tagIdForQuery
      }
    }
    request(data).then(res => {
      console.log(res)
      if (res.data.code === 200) {

        that.setData({
          fileList: res.data.data
        })
      } else if (res.data.code === 401) {
        $Message({
          content: '页面过期',
          type: 'error'
        })
      }
    })
  },

  //获取所有标签
  queryTagList: function() {
    var that = this
    const data = {
      url: '/v3/file/all_tag',
      method: 'POST',
      param: {}
    }
    request(data).then(res => {
      if (res.data.code === 200) {
        var list = res.data.data;
        that.setData({
          categories: res.data.data
        })

        that.data.categories.forEach(p => {
          let tagId = p.tagId;
          let tagName = p.tagName;
          var tag = {
            id: tagId,
            name: tagName
          }
          that.data.tagList.push(tag)
        })

        that.setData({
          tagList: that.data.tagList
        })


      } else if (res.data.code === 401) {
        $Message({
          content: '页面过期',
          type: 'error'
        })
      }
    })
  },

  goToDocSetPage: function(e) {
    
    wx.navigateTo({
      url: '../' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page + '?fileId=' + e.currentTarget.dataset.fileid + "&fileName=" + e.currentTarget.dataset.filename + "&qiniuUrl=" + e.currentTarget.dataset.qiniuurl + "&fileFrom=" + "library" + "&totalPage=" + e.currentTarget.dataset.total
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})